Refactor to instanceof pattern variable#2017
Refactor to instanceof pattern variable#2017arefbehboudi wants to merge 2 commits intospring-projects:mainfrom
Conversation
|
Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. Also - |
|
But how can I change the commit message after the push? |
|
git commit --amend and then git push -f ... |
|
Thank you! |
The constructor for DefaultTransaction now requires a name and `nested` flag. Closes spring-projects#2016 Signed-off-by: arefbehboudi <behboodiaref@gmail.com>
Closes spring-projects#2018 Signed-off-by: arefbehboudi <behboodiaref@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors multiple instanceof checks across Spring Data Couchbase to use Java’s pattern matching for instanceof, reducing explicit casts and improving readability in query, conversion, and reactive operation code paths.
Changes:
- Replaced
instanceof+ cast patterns withinstanceof Type varacross query building, mapping/conversion, and reactive operation support classes. - Updated a ternary
instanceofbranch in reactive query execution to bind the pattern variable. - Modified
CouchbaseTransactionStatusto call a differentDefaultTransactionStatussuperclass constructor (adds additional arguments).
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/java/org/springframework/data/couchbase/transaction/CouchbaseTransactionStatus.java | Changes superclass constructor invocation (beyond instanceof refactor). |
| src/main/java/org/springframework/data/couchbase/core/query/StringQuery.java | Uses pattern variables for JsonArray parameter handling. |
| src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java | Uses pattern variables for JsonArray/List in in(...) handling. |
| src/main/java/org/springframework/data/couchbase/core/query/OptionsBuilder.java | Uses pattern variables for query/tx parameter types. |
| src/main/java/org/springframework/data/couchbase/core/query/N1QLExpression.java | Uses pattern variable when building paths from N1QLExpression components. |
| src/main/java/org/springframework/data/couchbase/core/query/Meta.java | Uses pattern variables for String blank checks in meta setters. |
| src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseList.java | Uses pattern variables when computing recursive size. |
| src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java | Uses pattern variable for IndexDefinitionHolder selection. |
| src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java | Uses pattern variable for recursive document encoding. |
| src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java | Uses pattern variables in document/list reads, string conversion, and classloader-aware handling. |
| src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java | Uses pattern variables for Number/String date conversion sources. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveUpsertByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveRangeScanOperationSupport.java | Uses pattern variables for runtime exception conversion in range scans. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveMutateInByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveInsertByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveFindFromReplicasByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperationSupport.java | Uses pattern variables for runtime exception conversion and result type branching. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveFindByAnalyticsOperationSupport.java | Uses pattern variables for runtime exception conversion in analytics queries. |
| src/main/java/org/springframework/data/couchbase/core/ReactiveExistsByIdOperationSupport.java | Uses pattern variable for runtime exception conversion. |
| src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java | Uses pattern variables for mapping context / application context checks. |
| src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java | Uses pattern variable for TransactionOperationFailedException translation. |
| src/main/java/org/springframework/data/couchbase/core/AbstractTemplateSupport.java | Uses pattern variable for CAS extraction from Number. |
Comments suppressed due to low confidence (3)
src/main/java/org/springframework/data/couchbase/core/query/Meta.java:90
setValueclaims to remove entries whenvalueis null/blank, but after removing it unconditionally callsput(...)with the same (null/blank) value. This makes the removal ineffective and contradicts the method’s Javadoc; consider returning early afterremove(...)or using anelsebeforeput(...).
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
this.values.remove(MetaKey.valueOf(key));
}
this.values.put(MetaKey.valueOf(key), value);
}
src/main/java/org/springframework/data/couchbase/core/query/Meta.java:98
- Same issue as above: when
valueis null/blank the code removes the key but then immediatelyputs the (null/blank) value back into the map. Use early return orelseso null/blank values actually remove the entry.
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
this.values.remove(key);
}
this.values.put(key, value);
}
src/main/java/org/springframework/data/couchbase/core/query/Meta.java:106
- Same remove-then-put behavior here means null/blank values are still stored in
valuesdespite the intent to remove them. Consider restructuring this block soput(...)only happens when the value is non-null and (for Strings) non-blank.
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
this.values.remove(key);
}
this.values.put(key, value);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext; | ||
| if (cmc.isAutoIndexCreation()) { | ||
| if (mappingContext instanceof CouchbaseMappingContext cmc) { | ||
| if (cmc.isAutoIndexCreation()) { |
There was a problem hiding this comment.
Indentation for the nested if uses spaces and is misaligned with the file’s tab-based indentation, which can cause noisy diffs and may violate formatting rules. Please reformat to match the surrounding indentation style.
| if (cmc.isAutoIndexCreation()) { | |
| if (cmc.isAutoIndexCreation()) { |
| Object value = optsJson.get("args"); | ||
| if(value instanceof JsonObject){ | ||
| txOptions.parameters((JsonObject)value); | ||
| }else if(value instanceof JsonArray) { | ||
| txOptions.parameters((JsonArray) value); | ||
| if(value instanceof JsonObject jsonObject){ | ||
| txOptions.parameters(jsonObject); | ||
| }else if(value instanceof JsonArray jsonArray) { | ||
| txOptions.parameters(jsonArray); | ||
| } else if(value != null) { |
There was a problem hiding this comment.
The updated if/else if conditions here are missing spaces (if(value ...){, }else if(...){). Reformat to the consistent style used elsewhere in the file (if (value ...) {, } else if (...) {).
| public CouchbaseTransactionStatus(Object transaction, boolean newTransaction, boolean newSynchronization, boolean readOnly, boolean debug, Object suspendedResources) { | ||
| super(null, | ||
| transaction, | ||
| newTransaction, | ||
| newSynchronization, | ||
| false, | ||
| readOnly, | ||
| debug, | ||
| suspendedResources); |
There was a problem hiding this comment.
This change goes beyond an instanceof pattern refactor: it switches to a different DefaultTransactionStatus superclass constructor and passes null / false as additional arguments. Please confirm the targeted Spring DefaultTransactionStatus signature and document why these values are correct here (and update the PR description accordingly), since an argument-order mismatch could silently create a status with a null transaction.
| if (context instanceof ConfigurableApplicationContext configurableApplicationContext && indexCreator != null) { | ||
| configurableApplicationContext.addApplicationListener(indexCreator); | ||
| if (mappingContext instanceof CouchbaseMappingContext cmc) { | ||
| cmc.setIndexCreator(indexCreator); |
There was a problem hiding this comment.
cmc.setIndexCreator(indexCreator); is indented with spaces and doesn’t align with the surrounding block indentation. Reformat to match the file’s indentation style to avoid formatting-only diffs and potential checkstyle issues.
| cmc.setIndexCreator(indexCreator); | |
| cmc.setIndexCreator(indexCreator); |
| options.parameters((JsonObject) query.getParameters()); | ||
| if (query.getParameters() instanceof JsonArray jsonArray && !jsonArray.isEmpty()) { | ||
| options.parameters(jsonArray); | ||
| } else if( query.getParameters() instanceof JsonObject jsonObject && !jsonObject.isEmpty()){ |
There was a problem hiding this comment.
Spacing in the updated else if condition is inconsistent (else if( query...){). Since this hunk is already being modified, please normalize it to match the surrounding style (else if (query... && ...) {).
| } else if( query.getParameters() instanceof JsonObject jsonObject && !jsonObject.isEmpty()){ | |
| } else if (query.getParameters() instanceof JsonObject jsonObject && !jsonObject.isEmpty()) { |
| } else if (o[0] instanceof JsonArray jsonArray) { | ||
| value[0] = jsonArray.toList().toArray(); | ||
| } else if (o[0] instanceof List list) { | ||
| List l = list; | ||
| value[0] = l.toArray(); | ||
| } |
There was a problem hiding this comment.
The new JsonArray branch has inconsistent indentation (spaces vs tabs) compared to the surrounding code, and the List branch introduces an unnecessary temporary variable (List l = list). Align indentation and simplify to value[0] = list.toArray() (optionally with List<?> to avoid raw types).
In some of our cases, we are currently using the traditional instanceof checks followed by explicit type casting. With the introduction of Pattern Matching in recent Java versions, we can refactor these checks to make the code more concise and readable.